package org.sdau.rotten.library.controller;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import org.sdau.rotten.library.entity.User;
import org.sdau.rotten.library.service.Impl.MailService;
import org.sdau.rotten.library.service.UserService;
import org.sdau.rotten.library.dao.UserDAO;
import org.sdau.rotten.library.DTO.JsonData;
import org.sdau.rotten.library.exception.NoUserException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Calendar;
import java.util.List;

@Slf4j
@Controller
@RequestMapping("/customer/adminUser")
public class AdminUserController {
    @Autowired
    private UserDAO userDao;
    @Resource(name = "userServiceImpl")
    private UserService userService;
    @Autowired
    private MailService mailService;
    @ResponseBody
    @GetMapping("/delete")
    private JsonData delete(@RequestParam("user_id") String UserId){
        log.warn(DateUtil.date(Calendar.getInstance()).toString()+"删除用户"+UserId);
        int back = userDao.delete(Integer.parseInt(UserId));

        if(back==0)
        {
            throw new NoUserException(611,"无此用户");
        }
        return JsonData.buildSuccess("删除成功");
    }

    @GetMapping("/select")
    public ModelAndView select(ModelAndView modelAndView)
    {
        log.info( DateUtil.date(Calendar.getInstance()).toString()+"查询了用户");
        List<User> users = userDao.selectAll();
        modelAndView.addObject("list",users);
        modelAndView.setViewName("list_user");
        return modelAndView;
    }


    @PostMapping("/update")
    public JsonData Update(@RequestParam("user_id") String oldUserId,
                           @RequestParam("new_user_id")String newUserId,
                           @RequestParam("password") String password,
                           @RequestParam("authority")String authority)
    {
      log.info(DateUtil.date(Calendar.getInstance()).toString()+"修改了"+oldUserId);
      int back  = userDao.update(Convert.toInt(oldUserId),Convert.toInt(newUserId),password,authority);
      if(back==0)
      {
          throw new NoUserException(611,"无此用户");
      }
      return JsonData.buildSuccess("修改成功");
    }


    @GetMapping("/listUser")
    public ModelAndView listUser(ModelAndView model,@RequestParam("userId") String userId,
                                 HttpServletRequest request) {
        System.out.println("调用了listUser");
        List<User> list = userService.search(userId);
        request.getSession().setAttribute("list",list);
        model.addObject("list", list);
        model.setViewName("all_user");

        return model;
    }


    @ResponseBody
    @GetMapping( "/hint")
    public JsonData hint(@RequestParam("student_id")String studentId) {
        User user = userDao.selectById(studentId);//精准查询
        String email = user.getEmail();
        mailService.sendSimpleMail(email,"摆烂图书馆系统提醒",
                "您有书即将到期,请及时归还"
        );

        return JsonData.buildSuccess("发送成功");
    }
}
